package com.ndood.core.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ndood.core.util.HttpRequestUtil.Result;

/**
 * 地址工具类，根据经纬度获取地址相关信息
 */
public class AddressUtil {
	
	/**
	 * 根据经纬度获取详细地址
	 * {
		    "status": "1",
		    "info": "OK",
		    "infocode": "10000",
		    "regeocode": {
		        "formatted_address": "上海市浦东新区花木街道四季雅苑(柳杉路)",
		        "addressComponent": {
		            "country": "中国",
		            "province": "上海市",
		            "city": [],
		            "citycode": "021",
		            "district": "浦东新区",
		            "adcode": "310115",
		            "township": "花木街道",
		            "towncode": "310115016000",
		            "neighborhood": {
		                "name": [],
		                "type": []
		            },
		            "building": {
		                "name": [],
		                "type": []
		            },
		            "streetNumber": {
		                "street": [],
		                "number": [],
		                "direction": [],
		                "distance": []
		            },
		            "businessAreas": [
		                []
		            ]
		        }
		    }
		}
	 */
	@SuppressWarnings("unchecked")
	public static Map<String,String> getAddress(String location_x,String location_y){
		Result result = HttpRequestUtil.doGet("http://restapi.amap.com/v3/geocode/regeo?output=json&location="+location_y+","+location_x+"&key=8d4cacd2077d5e128b24bff5f7b3215c&radius=1");
		Map<String,Object> rs = new Gson().fromJson(result.getResult(), new TypeToken<HashMap<String,Object>>(){}.getType());
		Map<String,Object> regeocode = (Map<String, Object>) rs.get("regeocode");
		Map<String, Object> addressComponent = (Map<String, Object>)regeocode.get("addressComponent");
		Map<String,String> destMap = new HashMap<String,String>();
		String country = (String) addressComponent.get("country");
		String province = (String) addressComponent.get("province");
		Object city = addressComponent.get("city");
		if(city instanceof ArrayList || city==null) city = province; // 直辖市
		String citycode = (String) addressComponent.get("citycode");
		String district = (String) addressComponent.get("district");
		String township = (String) addressComponent.get("township");
		String towncode = (String) addressComponent.get("towncode");
		String adcode = (String) addressComponent.get("adcode");
		destMap.put("country", UnicodeUtil.encode(country));
		destMap.put("province", UnicodeUtil.encode(province));
		destMap.put("city", UnicodeUtil.encode((String)city));
		destMap.put("citycode", citycode);
		destMap.put("district", UnicodeUtil.encode(district));
		destMap.put("township", UnicodeUtil.encode(township));
		destMap.put("towncode", towncode);
		destMap.put("adcode", adcode);
		return destMap;
	}
	
	public static void main(String[] args) {
		Map<String, String> map =  getAddress("31.218807", "121.571967"); 
		System.out.println(new Gson().toJson(map));
	}
}
